<?php
	class POCO extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model('PO_model_1','PO');
			$this->load->model('PO_model','PO_model');
			$this->load->model('User_model','User');
			$this->load->model('Cabang_model','Cabang');
			$this->load->model('Penjualan_model','Penjualan');
			$this->load->model('Laporan_model','Laporan');
		}
		function index(){
			redirect('PO');
			//$this->load->view('halaman/po');
		}
		function PO_INSERT(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
				
				$data['halaman']	=	"po";
				$data['barang']	=	$this->PO->get_barang();
				
				$data['no_po']	=	$this->PO->get_nopo($data['intid_cabang'],$data['intid_week'],'PO');
				/*
				$this->load->view('halaman/head');
				$this->load->view('halaman/header');
				$this->load->view('halaman/js',$data);
				$this->load->view('halaman/index',$data);
				*/
				$this->load->view('halaman/po_header',$data);				
				$this->load->view('halaman/js',$data);				
				$this->load->view('halaman/po_hal1',$data);				
			}
		function proses_po(){
			if($_POST){
				//echo "Loading...<br />";
				$data = array('no_po' => $this->input->post('no_po'),
								'intid_cabang' => $this->input->post('intid_cabang'),
								'intid_week' => $this->input->post('intid_week'),
								);
				$this->db->trans_start();
				$this->PO->insert_DataPO(1,$data);
				$this->db->trans_complete();
				
				$barang = $this->input->post('barang');
				for($i=0;$i<sizeof($barang);$i++){
					if(isset($barang[$i]['intquantity']) and $barang[$i]['intquantity'] != 0){
						$data = array('no_po' => $barang[$i]['no_po'],
										'intid_barang' => $barang[$i]['intid_barang'],
										'quantity' => $barang[$i]['intquantity'],
										'status'	=> 1,
										'keterangan' => $barang[$i]['keterangan']);
						$this->PO->insert_DataPO(2,$data);
						$this->db->trans_complete();
						}
					}
				//echo "Done...!";
				if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('PO');
					}else{
					$this->db->trans_commit();	
					redirect('POCO/get_PO/?no='.$this->input->post('no_po'));
					}
				}else{
					redirect('home');
					}
			}
		function get_PO(){
			$no = $this->input->get('no');
			if($no !=''){
				$query	=	$this->PO->OpenDB('select pod.*,
									  b.strnama,
									  (select strnama_cabang 
									   	from cabang c inner join po_ po on po.intid_cabang = c.intid_cabang where po.no_po = pod.no_po group by c.intid_cabang) strnama_cabang,
									  (select time 
									   	from  po_ po where po.no_po = pod.no_po group by po.no_po) timeNow,
									  b.intid_jsatuan
									  from po_detail_ pod inner join barang b on b.intid_barang = pod.intid_barang  
									  where pod.no_po = "'.$no.'" order by b.strnama asc');
				$data['query'] = $query->result();
				}else{
					$data['query'] = $this->PO->DataPO();
				}
			$this->load->view('halaman/test',$data);
			}
		function DataSPKB(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			//pagination
			$hal = $this->input->get('per_page');
			if($hal == "" or !isset($hal)){
				$hal = 0;
			}
			$limit = 15;
				
				$query = $this->PO->OpenDB('select count(*) total from spkb inner join cabang on cabang.intid_cabang = spkb.intid_cabang 
														where spkb.no_sj = ""');
					$result = $query->result();
					$total = $result[0]->total;
					
				$this->load->library('pagination');
				$config['base_url']  = base_url()."POCO/DataSPKB/?cabang=0";
				$config['total_rows']	=	$total;
				$config['per_page'] 	=	$limit;
				$config['num_links']	=	6;
				$config['first_link']	=	"awal ";
				$config['last_link']	=	"akhir";
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$data['pagination'] = "<div align='center' style='display:block;margin:5px auto 5px auto;width:100%; height:10px;'>".$this->pagination->create_links()."</div>";
				
			
			//pagination
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$var =	"";
			$var .= "<div><form method='POST' action='".base_url()."POCO/DataSPKB_SEARCH/ '>Masukan nama cabang<input type='text' name='cabang' size='20' /><input type='submit' name='bttnsubmit' value='search' /></form></div>";
			$var .= $data['pagination'];
			$query = $this->PO->OpenDB('select spkb.*,spkb.time waktu,(select strnama_cabang from cabang where cabang.intid_cabang= spkb.intid_cabang)strnama_cabang from spkb where no_sj is NOT NULL order by waktu desc limit '.$hal.','.$limit.'');
			
			$var .= "<table border='1' style='background:#FFF;' width='100%'>";
			$var .= "<tr>
				<th>No.</th>
				<th>CABANG</th>
				<th>TIME</th>
				<th>NO SPKB</th>
				<th>ACTION</th>
			</tr>";
			$no = 1;
			foreach($query->result() as $row){
				$var .='<tr>
					<td> '.$no++.'.</td>
					<td>'.$row->strnama_cabang.'</td>
					<td>'.$row->waktu.'</td>
					<td>'.$row->no_spkb.'</td>
					<td><a href="'.base_url().'POCO/GET_SPKB/?no='.$row->no_spkb.'">View</a></td>
				</tr>';
				}
			$var .= "<tr>
				<td colspan = '4'>&nbsp;</td>
			</tr>";
			$var .="</table>";
			$var .= $data['pagination'];
			
			$data['tampilan'] =	$var;
			$data['url'] = "";
			$this->load->view('halaman/po_header',$data);
			$this->load->view('halaman/SPKB_hal2',$data);
			}
		function DataSPKB_SEARCH(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			//pagination
			$hal = $this->input->get('per_page');
			if($hal == "" or !isset($hal)){
				$hal = 0;
			}
			$limit = 15;
			//post data from DataSPKB
				if($_POST){
					$varcabang = $this->input->post('cabang');
				}else{
					$varcabang = $this->input->get('cabang');
				}
				$query = $this->PO->OpenDB('select count(*) total from spkb inner join cabang on cabang.intid_cabang = spkb.intid_cabang 
														where spkb.no_sj = "" and cabang.strnama_cabang like "%'.$varcabang.'%"');
					$result = $query->result();
					$total = $result[0]->total;
					
				$this->load->library('pagination');
				$config['base_url']  = base_url()."POCO/DataSPKB_SEARCH/?cabang=".$varcabang."";
				$config['total_rows']	=	$total;
				$config['per_page'] 	=	$limit;
				$config['num_links']	=	6;
				$config['first_link']	=	"awal ";
				$config['last_link']	=	"akhir";
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$data['pagination'] = "<div align='center' style='display:block;margin:5px auto 5px auto;width:100%; height:10px;'>".$this->pagination->create_links()."</div>";
				
			
			//pagination
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$var =	"";
			$query = $this->PO->OpenDB('select spkb.*,spkb.time waktu,b.strnama_cabang from spkb inner join cabang b on b.intid_cabang = spkb.intid_cabang where no_sj is NOT NULL and b.strnama_cabang like "'.$varcabang.'%" order by waktu desc limit '.$hal.','.$limit.'');
			
			$var .= "<table border='1' style='background:#FFF;' width='100%'>";
			$var .= "<tr>
				<th>No.</th>
				<th>CABANG</th>
				<th>TIME</th>
				<th>NO SPKB</th>
				<th>ACTION</th>
			</tr>";
			$no = 1;
			foreach($query->result() as $row){
				$var .='<tr>
					<td> '.$no++.'.</td>
					<td>'.$row->strnama_cabang.'</td>
					<td>'.$row->waktu.'</td>
					<td>'.$row->no_spkb.'</td>
					<td><a href="'.base_url().'POCO/GET_SPKB/?no='.$row->no_spkb.'">View</a></td>
				</tr>';
				}
			$var .= "<tr>
				<td colspan = '4'>&nbsp;</td>
			</tr>";
			$var .="</table>";
			$data['tampilan'] =	$var;
			$data['url'] = "";
			$this->load->view('halaman/po_header',$data);
			$this->load->view('halaman/SPKB_hal2',$data);
			}
		function SPKB_INSERT(){
			$week = $this->Penjualan->selectWeek();
			$var = "";
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			$temp = "";	
			$intid_week	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$intid_cabang	=	$cabang[0]->intid_cabang;
				
			$query = $this->PO->openDB('select *,po.time waktu,(select strnama_cabang from cabang where intid_cabang = po.intid_cabang )strnama_cabang 
																from po_ po where 
																	 po.is_sj = 0 and (po.no_spkb is NULL or po.no_spkb = "") order by time desc LIMIT 0, 10');
			
			$var = "<form method='post' action='".base_url()."POCO/SPKB'>";
					$var .= "<br/>
					<table border='1' width='100%' style='background-color:#FFF'>";
					$var .= "<tr>
						<th width='25%'>Cabang</th>
						<th width='35'>Week</th>
						<th width='25%'>Nomor PO</th>
						<th width='35'>Waktu PO</th>
						<th>Action</th>
						</tr>";
					foreach($query->result() as $rok){
						$temp .= "<tr>
							<td align='center'>".$rok->strnama_cabang."</td>
							<td align='center'>".$rok->intid_week."</td>
							<td align='right'>".$rok->no_po."</td>
							<td align='center'>".$rok->waktu."</td>
							<td><a href='".base_url()."POCO/get_PO/?no=".$rok->no_po."'>VIEW</a></td>
							</tr>";
						}
						if($temp != ""){
							$var .= $temp;
							//$var .= "<tr><td colspan='5' align='right'><input type='submit' name='submit' value='LIHAT PO' /></td></tr>";
					}else{
								$var .= "<tr><td colspan='5'>TIDAK ADA PO!</td></tr>";
								}
					$var .= "</table></form>";
			
			$data['intid_week']	=	$week[0]->intid_week;
			
			$data['tampilkan'] = $var;
			$this->load->view('halaman/po_header',$data);
			//$this->load->view('halaman/method_spkb',$data);
			$this->load->view('halaman/SPKB_hal1',$data);
		}
		//AJAX CALL
		function FORM_SPKB_RESULT(){
			$var = "Akses Dilarang!! Contact Admin Pusat!";
			$temp = "";
			$ajax	=	$this->input->post('ajax');
			$strnama_cabang = $this->input->post('strnama_cabang');
			$intid_week = $this->input->post('intid_week');
			//Ajax Time 
			if($ajax == 1){
				$cabang	=	$this->User->getCabang($strnama_cabang);			
				$intid_cabang = $cabang[0]->intid_cabang;
				$query = $this->PO->openDB('select *,po.time waktu,(select strnama_cabang from cabang where intid_cabang = '.$intid_cabang.')strnama_cabang from po_ po where po.intid_cabang = '.$intid_cabang.' 
																	and po.is_sj = 0 and (po.no_spkb is NULL or po.no_spkb = "")order by time ASC');
					$var = "<form method='post' action='".base_url()."POCO/SPKB'>";
					$var .= "<h2>PO ".$strnama_cabang.", week : ".$intid_week."</h2><table border='1' width='100%' style='background-color:#FFF'>";
					$var .= "<tr>
						<th width='10%'>Pilih</th>
						<th width='25%'>Cabang</th>
						<th width='35'>Week</th>
						<th width='25%'>Nomor PO</th>
						<th width='35'>Waktu PO</th>
						<th>Action</th>
						</tr>";
					foreach($query->result() as $rok){
						$temp .= "<tr>
							<td align='center'><input type='checkbox' name='po[]' value='".$rok->no_po."' /></td>
							<td align='center'>".$rok->strnama_cabang."</td>
							<td align='center'>".$rok->intid_week."</td>
							<td align='right'>".$rok->no_po."</td>
							<td align='center'>".$rok->waktu."</td>
							<td><a href='".base_url()."POCO/get_PO/?no=".$rok->no_po."'>VIEW</a></td>
							</tr>";
						}
						if($temp != ""){
							$var .= $temp;
							$var .= "<tr><td colspan='6' align='right'><input type='submit' name='submit' value='LIHAT PO' /></td></tr>";
					}else{
								$var .= "<tr><td colspan='6'>TIDAK ADA PO!</td></tr>";
								}
					$var .= "</table></form>";
					echo $var;
				}else{
					echo $var;
				}
			}
		function SPKB(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			//$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			
			$arr	= array();
			$data['input_type']	= "";
			$po		= $this->input->post('po');
			$no_po	=	array();
			$query0 = $this->PO->OpenDB('select intid_cabang, (select strnama_cabang from cabang where intid_cabang = po.intid_cabang) strnama_cabang from po_ po where po.no_po = "'.$po[0].'"');
			$rock= $query0->result();
			
			$data['no_SPKB']	=	$this->PO->get_nopo($rock[0]->intid_cabang,$data['intid_week'],'SPKB');
			$data['strnama_cabang'] = $rock[0]->strnama_cabang;
			$data['intid_cabang'] = $rock[0]->intid_cabang;
			for($i=0;$i<sizeof($po);$i++){
				$data['input_type']	.=	"<input type='hidden' name='PO[".$i."]' value='".$po[$i]."' />";
				if($po[$i] != ''){
					$query = $this->db->query('select pod.*, b.strnama, b.intid_jsatuan from po_detail_ pod inner join barang b on b.intid_barang = pod.intid_barang where pod.no_po = "'.$po[$i].'" order by b.strnama asc');
					foreach($query->result() as $row){
							$arr[] = array('intid_barang' => $row->intid_barang,
										   'strnama'	=>$row->strnama,
										   'quantity'	=> $row->quantity,
										   'intid_jsatuan' => $row->intid_jsatuan,
										   'keterangan' => $row->keterangan,
										   'no_po' => $po[$i]);
						}
					}
				}
				$data['arr']	=	$arr;
				$data['po']	=	$po;
				///////-------
				$this->load->view('halaman/head',$data);
				$this->load->view('halaman/SPKB',$data);
			}
		//proses penginputan SPKB
		function Proses_SPKB(){
			$no_spkb = $this->input->post('SPKB');
			$po	=	$this->input->post('PO');
			$intid_cabang = $this->input->post('intid_cabang');
			$week = $this->input->post('intid_week');
			
			$namaBarang	= $this->input->post('namaBarang');
			$intidBarang= $this->input->post('intidBarang');
			$quantity	= $this->input->post('quantity');
			$keterangan	= $this->input->post('keterangan');
			
			$this->db->trans_start();
			$this->db->query('insert into spkb values("'.$no_spkb.'", "", "'.date('Y-m-d H:i:s').'","","","","'.$week.'",0,"'.$intid_cabang.'","","")');
			$this->db->trans_complete();
			for($i=0;$i<sizeof($intidBarang);$i++){
				$this->db->query('insert into spkb_detail values ("'.$no_spkb.'", "'.$intidBarang[$i].'","'.$quantity[$i].'","'.$keterangan[$i].'")');
				$this->db->trans_complete();
				}
			
			for($i=0;$i<sizeof($po);$i++){
				$select = 'update po_ set no_spkb="'.$no_spkb.'" where no_po = "'.$po[$i].'"';
				$this->db->trans_complete();
				$this->db->query($select);
				}
			if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('POCO/SPKB');
				}else{
					$this->db->trans_commit();	
					redirect('POCO/GET_SPKB/?no='.$no_spkb.'');
				}
			}
		function GET_SPKB(){
			$no_spkb	= $this->input->get('no');
			$var	=	"";
			$query	= $this->PO->openDB('select spb.*,b.strnama,b.intid_jsatuan from spkb_detail spb inner join barang b on b.intid_barang = spb.intid_barang where spb.no_spkb = "'.$no_spkb.'" and spb.quantity != 0 order by b.strnama asc');
			$query2	= $this->PO->openDB('select po.no_po,spkb.no_spkb,(select strnama_cabang from cabang where cabang.intid_cabang = spkb.intid_cabang)strnama_cabang, spkb.time waktu from spkb left join po_ po on po.no_spkb = spkb.no_spkb where spkb.no_spkb like "'.$no_spkb.'"');
			$rok = $query2->result();
			$temp = "";
			/*
			if(isset($rok[0]->waktu)){
				$temp = date('d - m - Y',strtotime($rok[0]->waktu));
			}else{
				$temp = "00 - 00 - 0000";
			}
			if(isset($rok[0]->strnama_cabang)){
				$temp2 = strtoupper($rok[0]->strnama_cabang);
			}else{
				$temp2 = "--tidak ada cabang --";
			}
			*/
			if(isset($rok[0]->waktu)){
				$temp = date('d - m - Y',strtotime($rok[0]->waktu));
				}else{
					$temp = "0000-00-00";
				}
				if(isset($rok[0]->strnama_cabang)){
					$temp2 = strtoupper($rok[0]->strnama_cabang);
				}else{
					$temp2 = "";
				}
				$var .= "<table>
			<tr><td>BANDUNG</td><td>,</td><td>".$temp."</td></tr>
			<tr><td><b>CABANG</b></td><td>:</td><td><b>".$temp2."</b></td></tr>
			";
			foreach($query2->result() as $rok){
				$var .= "<tr><td><b>NO PO</b></td><td>:</td><td><b>".$rok->no_po."</b></td></tr>";
				}
			
			$var .= "<tr><td><b>NO SPKB</b></td><td>:</td><td><b style='font-size:20px;'>".$no_spkb."</b></td></tr>";
			$var .="</table>";
			$var .= "<table border='1' width='100%'>";
			$var .="<tr>
					<th rowspan='2'>NO.</th>
					<th rowspan='2'>NAMA BARANG</th>
					<th colspan='2'>JUMLAH</th>
					<th rowspan='2'>KETERANGAN</th>
				</tr>";
			$var .="<tr>
					<th>PCS</th>
					<th>SET</th>
					</tr>";
			$no = 1;
			$total =0;
			$totalQ = 0;
			$totalQ2 = 0;
			foreach($query->result() as $row){
				$var	.=	"<tr>
					<td align='center'>".$no++."</td>
					<td>".$row->strnama."</td>";
				if($row->intid_jsatuan == 2){
					$var .="<td align='center'>".$row->quantity."</td><td align='center'>0</td>";
				}else{
					$var .="<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
				}
					$var.="<td>".strtoupper($row->keterangan)."</td>
				</tr>";
				if($row->intid_jsatuan == 2){
					$totalQ = $totalQ + $row->quantity;
					}else{
					$totalQ2 = $totalQ2 + $row->quantity;
					}
				}	
				$var .= "<tr>
					<td colspan ='2' align='right'>JUMLAH</td>
					<td align='center'><b>".$totalQ."</b></td>
					<td align='center'><b>".$totalQ2."</b></td>
					<td>&nbsp;</td>
				</tr>";
			$var .="</table>";
			$var .="<table width='100%'>
					<tr>
						<td colspan='4'>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width='20%' align='center'>ADM GUDANG</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width='20%' align='center'>(..............................)</td>
					</tr>
				";
			$data['tampilan'] =	$var;
			$data['url'] = base_url()."POCO/GET_EXCEL_SPKB/?no=".$no_spkb."";
			$this->load->view('halaman/print',$data);
			}
		function SJ_INSERT(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$hal = $this->input->get('per_page');
			if($hal == "" or !isset($hal)){
				$hal = 0;
			}
			$limit = 15;
				
				$query = $this->PO->OpenDB('select count(*) total from spkb inner join cabang on cabang.intid_cabang = spkb.intid_cabang 
														where spkb.no_sj = ""');
					$result = $query->result();
					$total = $result[0]->total;
					
				$this->load->library('pagination');
				$config['base_url']  = base_url()."POCO/SJ_INSERT/?cabang=0";
				$config['total_rows']	=	$total;
				$config['per_page'] 	=	$limit;
				$config['num_links']	=	6;
				$config['first_link']	=	"awal ";
				$config['last_link']	=	"akhir";
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$data['pagination'] = $this->pagination->create_links();
					
				$data['query']	=	$this->PO->SPKB_SJ($hal,$limit);
				$this->load->view('halaman/po_header');
				$this->load->view('halaman/SJ_hal1',$data);
			}
		//SJ_INSERT SEARCHING
		function SJ_INSERT_SEARCH(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$limit = 15;
				
				if($_POST){							
					$varcabang = $this->input->post('cabang');
					$hal	=	0;
				}else{
					$varcabang = $this->input->get('cabang');
					$hal	=	$this->input->get('per_page');
					if($hal == null){
						$hal = 0;
					}
				}
					$data['query'] = $this->PO->SPKB_SJ_SEARCH($varcabang,$hal,$limit);

					$query = $this->PO->OpenDB('select count(*) total from spkb inner join cabang on cabang.intid_cabang = spkb.intid_cabang 
														where cabang.strnama_cabang like "'.$varcabang.'" and spkb.no_sj = ""');
					$result = $query->result();
					$total = $result[0]->total;
					
				$this->load->library('pagination');
				$config['base_url']  = base_url()."POCO/SJ_INSERT_SEARCH/?cabang=".$varcabang."";
				$config['total_rows']	=	$total;
				$config['per_page'] 	=	$limit;
				$config['num_links']	=	6;
				$config['first_link']	=	"awal ";
				$config['last_link']	=	"akhir";
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$data['pagination'] = $this->pagination->create_links();
				
				
					$this->load->view('halaman/po_header');
					$this->load->view('halaman/SJ_hal1',$data);
			}
		function Proses_SJ(){
				$data['no_spkb']	=	$this->input->get('no');
				
				$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
				$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
				$week = $this->Penjualan->selectWeek();
				
				$data['halaman'] = 'SJ';	
				$data['barang']	=	$this->PO->get_barang();
					
				$data['intid_week']	=	$week[0]->intid_week;
				$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
				
				$query = $this->PO->OpenDB('select intid_cabang, (select strnama_cabang from cabang where cabang.intid_cabang = spkb.intid_cabang) strnama_cabang,
																  (select po.time waktu from po_ po where po.no_spkb = spkb.no_spkb order by waktu asc limit 0,1)tgl_po from spkb where no_spkb = "'.$data['no_spkb'].'" ');
				$result = $query->result();
				
				$data['intid_cabang']	=	$result[0]->intid_cabang;
				$data['strnama_cabang_spkb'] = $result[0]->strnama_cabang;
				$data['waktu'] = date('Y-m-d',strtotime($result[0]->tgl_po));
					
				$data['no_po']	=	$this->PO->get_nopo($result[0]->intid_cabang,$data['intid_week'],'SJ');
				$data['query']	=	$this->PO->OpenDB('select spd.*,barang.strnama,barang.intid_jsatuan 
														  from spkb_detail spd inner join barang on barang.intid_barang = spd.intid_barang 
														  where spd.no_spkb="'.$data['no_spkb'].'" and spd.quantity != 0 order by barang.strnama asc');	
				
					
					$this->load->view('halaman/po_header');
					$this->load->view('halaman/js',$data);
					$this->load->view('halaman/SJ_hal2',$data);
			}
		function Proses_SJ_STEP2(){
			if($_POST){
				$no_sj	=	$this->input->post('no_sj');
				$no_spkb	=	$this->input->post('no_spkb');
				$namaBarang	=	$this->input->post('namaBarang');
				$intid	=	$this->input->post('intid');
				$intid_week = $this->input->post('intid_week');
				$quantity	=	$this->input->post('quantity');
				$keterangan	=	$this->input->post('keterangan');
				$tgl_order = $this->input->post('tgl_order');
				$tgl_kirim = $this->input->post('tgl_kirim');
				$via	=	$this->input->post('via');
				
				$TnamaBarang	=	$this->input->post('TnamaBarang');
				$Tintid_barang	=	$this->input->post('Tintid_barang');
				$Tquantity	=	$this->input->post('Tquantity');
				$Tketerangan	=	$this->input->post('Tketerangan');
				
				//mulai update data table
				$this->db->trans_start();
				for($i=0; $i<sizeof($intid);$i++){
					//echo $no_spkb.",".$intid[$i].", ".$namaBarang[$i].",".$quantity[$i].",".$keterangan[$i]."<br />";
					$this->PO->OpenDB('update spkb_detail 
									 	set quantity = "'.$quantity[$i].'", keterangan = "'.$keterangan[$i].'"
										where intid_barang = "'.$intid[$i].'" and no_spkb = "'.$no_spkb.'"');
					$this->db->trans_complete();
					}
				//mulai insert
				for($i=0;$i<sizeof($Tintid_barang);$i++){
					//echo $no_spkb.",".$Tintid_barang[$i].", nama : ".$TnamaBarang[$i].",".$Tquantity[$i].",".$Tketerangan[$i]."::T<br />";
					$this->PO->OpenDB('insert into spkb_detail values("'.$no_spkb.'","'.$Tintid_barang[$i].'","'.$Tquantity[$i].'","'.$Tketerangan[$i].'")');
					$this->db->trans_complete();
					}
				//
				$this->PO->OpenDB('update spkb 
									 	set no_sj = "'.$no_sj.'", tgl_order = "'.$tgl_order.'", tgl_kirim = "'.$tgl_kirim.'", via = "'.$via.'",week_sj = "'.$intid_week.'"
										where no_spkb = "'.$no_spkb.'"');
				$this->db->trans_complete();
				if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('POCO/Proses_SJ/?no='.$no_spkb.'');	
				}else{
					$this->db->trans_commit();	
					redirect('POCO/GET_SJ/?no_sj='.$no_sj.'&no_spkb='.$no_spkb.'');	
				}
				//query = $this->PO->OpenDB('select no_spkb,intid_barang from spkb_detail where no_spkb ="'.$no_spkb.'" and quantity = 0');
				/*
				//untuk menghapus semua yang quantity spkb_detail ny 0 
				foreach($query->result() as $row){
					$this->PO->OpenDB('delete from spkb_detail where no_spkb = "'.$no_spkb.'" and intid_barang = "'.$row->intid_barang.'"');
					}
				*/
				}
			}	
		function Get_Barang(){
			$namaBarang = $this->input->post('namaBarang');
			$data['response'] = 'false';
			$query = $this->PO->OpenDB('select * from barang where strnama = "'.$namaBarang.'" and status_barang = 1');
			if( ! empty($query) )
			{
				$data['response'] = 'true';
				$data['message'] = array();
				foreach( $query as $row )
				{
					/*
					 $cabang = $this->User_model->getCabang($this->session->userdata('username'));
					if($cabang[0]->intid_wilayah == 1){
						$hrg = $row->intharga_jawa;
						$pv = $row->intpv_jawa;
						$um = $row->intum_jawa;
						$cicilan = $row->intcicilan_jawa;
					}else{
						$hrg = $row->intharga_luarjawa;
						$pv = $row->intpv_luarjawa;
						$um = $row->intum_luarjawa;
						$cicilan = $row->intcicilan_luarjawa;
					}
					*/
					$data['message'][] = array(
											'id'=>$row->intid_barang,
											'value'=>$row->strnama,
											);
				}
	
			}
	
	
			if('IS_AJAX')
			{
				echo json_encode($data);
	
			}
			else
			{
				$this->load->view('admin_views/autocomplete/index',$data);
			}
		
			}
		function GET_SJ(){
			$data['no_sj']	=	$this->input->get('no_sj');
			$data['no_spkb']	=	$this->input->get('no_spkb');
			$query2 = $this->PO->OpenDB('select intid_cabang, (select strnama_cabang from cabang where cabang.intid_cabang = spkb.intid_cabang) strnama_cabang, spkb.* from spkb where spkb.no_spkb = "'.$data['no_spkb'].'"');
			$rok = $query2->result();
			$var = "";
			$var .= "<div style='margin:0 auto 0 auto;font-size:30px;background:none;width:30%;' align='center'>SURAT JALAN</b><br />
			<c style='font-size:23px;'>".strtoupper($rok[0]->strnama_cabang)."<c></div><br />";
			$var .= "<table style='float:right; background:none; margin: -20px auto 0 auto;'>
				<tr>
					<td>Bandung</td><td>,</td><td>".date('d - m - Y',strtotime($rok[0]->tgl_kirim))."</td>
				</tr>
				<tr>
					<td>WEEK</td><td>:</td><td>".$rok[0]->week_sj."</td>
				</tr>
				<tr>
					<td>VIA</td><td>:</td><td>".strtoupper($rok[0]->via)."</td>
				</tr>
			</table>";
			$query 	=	$this->PO->OpenDB('select spk.*,b.strnama,b.intid_jsatuan from spkb_detail spk inner join barang b on b.intid_barang = spk.intid_barang where spk.no_spkb = "'.$data['no_spkb'].'" and spk.quantity != 0 order by b.strnama asc');
			$var .= '<div style="margin:50px auto auto auto;font-size:20px;">No Surat Jalan : '.$data['no_sj'].'</div>';
			$var .= '<table border="1" width="100%">';
			$var .= '<tr><th rowspan="2">NO SPKB</th>
						<th rowspan="2">NAMA BARANG</th>
						<th colspan="2">JUMLAH</th>
						<th rowspan="2">KETERANGAN</th>
						</tr>';
			$var .= '<tr>
				<th>PCS</th>
						<th>SET</th>
						</tr>';
			$total_Q=0;
			$total_Q2=0;
			$var .= '<tr><td align="center">'.$data['no_spkb'].'</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></h4>';
			
			foreach($query->result() as $row){
				$var .=	"<tr>
					<td>&nbsp;</td>
					<td>".$row->strnama."</td>";
					if($row->intid_jsatuan == 2){
						$var .="<td align='center'>".$row->quantity."</td><td align='center'>0</td>";
						$total_Q = $row->quantity + $total_Q;
					}else{
						$var .="<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
						$total_Q2 = $row->quantity + $total_Q2;
					}
				$var .="<td>".strtoupper($row->keterangan)."</td>
				</tr>";
				}
			
			$var .= '<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
			$var .= '<tr>
				<th align = "center" colspan="2">TOTAL PESANAN</th>
				<th>'.$total_Q.'</th>
				<th>'.$total_Q2.'</th>
				<th>&nbsp;</th>
						</tr>';
			$var .= '</table>';
			$var .= '<table width="100%" align="center">
				<tr><td colspan="9">Barang pesanan yang tidak ada di Surat Jalan berarti kondisi barang kosong di kantor pusat.</td></tr>
				<tr><td colspan="9">Barang yang telah dipesan tidak dapat dikembalikan.</td></tr>
				<tr><td colspan="9">Klaim barang hanya kami terima 1 hari dari pengiriman barang, 1 hari dari penerimaan jarak jauh.</td></tr>
				<tr><td colspan="9">Atas perhatian dan kerjasamanya kami ucapkan terima kasih</td></tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr>
				<td>&nbsp;</td>
				<td align="center">PENERIMA</td>
				<td>&nbsp;</td>
				<td align="center">PEMERIKSA</td>
				<td>&nbsp;</td>
				<td align="center">MENGETAHUI</td>
				<td>&nbsp;</td>
				<td align="center">ADM.GUDANG</td>
				<td>&nbsp;</td>
				</tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				</tr>
			</table>';
			$data['tampilan'] =	$var;
			$data['url'] = base_url().'POCO/GET_EXCEL_SJ/?no_sj='.$data['no_sj'].'&no_spkb='.$data['no_spkb'].'';
			$this->load->view('halaman/print',$data);
			}
		function DATA_SJ(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$var = "";
			$var .= "<table border='1' style='background-color:#FFF;'>";
			$var .= "<tr>
				<th>NO.</th>
				<th>CABANG</th>
				<th>WEEK</th>
				<th>NO</th>
				<th>WAKTU</th>
				<th>ACTION</th>
			</tr>";
			$i = 1;
			$query 	=	$this->PO->OpenDB('select spkb.*,spkb.time waktu,
										  (select strnama_cabang from cabang where cabang.intid_cabang = spkb.intid_cabang) strnama_cabang
										  from spkb where spkb.intid_cabang = "'.$data['intid_cabang'].'" 
										  	and spkb.no_sj IS NOT NULL
											order by waktu desc');			
			foreach($query->result() as $row){
				$var .= "<tr>
					<td>".$i++."</td>
					<td>".$row->strnama_cabang."</td>
					<td>".$row->week."</td>
					<td>".$row->no_sj."</td>
					<td>".$row->waktu."</td>
					<td>##</td>
				</tr>";
				}
			$var .= "</table>";
			$data['tampilan'] = $var;
			$this->load->view('halaman/po_header');
			$this->load->view('halaman/SJ_hal3',$data);
			}
		function GET_EXCEL_SJ(){
			$var = "tidak ad data";
			$no_sj = $this->input->get('no_sj');
			$data['no_sj']	=	$this->input->get('no_sj');
			$data['no_spkb']	=	$this->input->get('no_spkb');
			$query2 = $this->PO->OpenDB('select intid_cabang, (select strnama_cabang from cabang where cabang.intid_cabang = spkb.intid_cabang) strnama_cabang, spkb.* from spkb where spkb.no_spkb = "'.$data['no_spkb'].'"');
			$rok = $query2->result();
			//$var = "no_sj ".$data['no_sj'].", no_spkb ".$data['no_spkb'];
			$var = "
			<div style='display:block; height:80px;'><img src='".base_url()."images/logo.jpg' align='left' /></div>
			<div style='background:none; width:300px; float:right;'>
			<ul style='list-style-type:none; float:left;'>
				<li>CABANG : ".$rok[0]->strnama_cabang."</li>
				<li>WEEK : ".$rok[0]->week."</li>
				<li>Via : ".$rok[0]->via."</li>
				<li>TGl PO : ".$rok[0]->tgl_order."</li>
				<li>TGL Kirim ".$rok[0]->tgl_kirim."</li>
			</ul></div>";
			$query 	=	$this->PO->OpenDB('select spk.*,b.strnama,b.intid_jsatuan from spkb_detail spk inner join barang b on b.intid_barang = spk.intid_barang where spk.no_spkb = "'.$data['no_spkb'].'" and spk.quantity !=0 order by b.strnama asc');
			$var .= '<div style="margin:100px auto auto auto;">No Surat Jalan : '.$data['no_sj'].'</div>';
			$var .= '<table border="1" width="100%">';
			$var .= '<tr><th rowspan="2">NO SPKB / STTB</th>
						<th rowspan="2">Nama Barang</th>
						<th colspan="2">Jumlah</th>
						<th rowspan="2">Keterangan</th>
						</tr>';
			$var .= '<tr>
				<th>PCS</th>
						<th>Set</th>
						</tr>';
			$total_Q=0;
			$total_Q2=0;
			$var .= '<tr><td align="center">'.$data['no_spkb'].'</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></h4>';
			
			foreach($query->result() as $row){
				$var .=	"<tr>
					<td>&nbsp;</td>
					<td>".$row->strnama."</td>";
					if($row->intid_jsatuan == 2){
						$var .="<td align='center'>".$row->quantity."</td><td align='center'>0</td>";
						$total_Q = $row->quantity + $total_Q;
					}else{
						$var .="<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
						$total_Q2 = $row->quantity + $total_Q2;
					}
				$var .="<td>".$row->keterangan."</td>
				</tr>";
				}
			
			$var .= '<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
			$var .= '<tr>
				<th align = "center" colspan="2">TOTAL PESANAN</th>
				<th>'.$total_Q.'</th>
				<th>'.$total_Q2.'</th>
				<th>&nbsp;</th>
						</tr>';
			$var .= '</table>';
			$var .= '<table width="100%" align="center">
				<tr><td colspan="9">Barang pesanan yang tidak ada di Surat Jalan berarti kondisi barang kosong di kantor pusat.</td></tr>
				<tr><td colspan="9">Barang yang telah dipesan tidak dapat dikembalikan.</td></tr>
				<tr><td colspan="9">Klaim barang hanya kami terima 1 hari dari pengiriman barang, 1 hari dari penerimaan jarak jauh.</td></tr>
				<tr><td colspan="9">Atas perhatian dan kerjasamanya kami ucapkan terima kasih</td></tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr>
				<td>&nbsp;</td>
				<td align="center">PENERIMA</td>
				<td>&nbsp;</td>
				<td align="center">PEMERIKSA</td>
				<td>&nbsp;</td>
				<td align="center">MENGETAHUI</td>
				<td>&nbsp;</td>
				<td align="center">ADM.GUDANG</td>
				<td>&nbsp;</td>
				</tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				</tr>
			</table>';
			
			$data['tampilan'] = $var;
			$this->load->view('halaman/excel',$data);
			}
		function GET_EXCEL_SPKB(){
			$no_spkb	= $this->input->get('no');
			$var	=	"";
			$query	= $this->PO->openDB('select spb.*,b.strnama,b.intid_jsatuan from spkb_detail spb inner join barang b on b.intid_barang = spb.intid_barang where spb.no_spkb = "'.$no_spkb.'"');
			$query2	= $this->PO->openDB('select po.no_po,(select strnama_cabang from cabang where cabang.intid_cabang = po.intid_cabang)strnama_cabang, po.time waktu from po_ po where po.no_spkb = "'.$no_spkb.'"');
			$rok = $query2->result();
			$var = "<div style='display:block; height:80px;'><img src='".base_url()."images/logo.jpg' align='left' /></div>";			
			$var .= "<div style='float:right; margin-right:60px;'>".$rok[0]->waktu."</div><br />"; 
			$var .= "<div style='float:right; margin-right:60px;'>Cabang : ".$rok[0]->strnama_cabang."</div>"; 
			foreach($query2->result() as $rok){
				$var .= "NO PO : ".$rok->no_po."<br/>";
				}
			
			$var .= "<p><b>NO SPKB : ".$no_spkb."</b></p>";
			$var .= "<table border='1' width='100%'>";
			$var .="<tr>
					<th rowspan='2'>No.</th>
					<th rowspan='2'>Nama Barang</th>
					<th colspan='2'>Jumlah</th>
					<th rowspan='2'>Keterangan</th>
				</tr>";
			$var .="<tr>
					<th>PCS</th>
					<th>SET</th>
					</tr>";
			$no = 1;
			$total =0;
			$totalQ = 0;
			$totalQ2 = 0;
			foreach($query->result() as $row){
				$var	.=	"<tr>
					<td>".$no++."</td>
					<td>".$row->strnama."</td>";
				if($row->intid_jsatuan == 2){
					$var .="<td align='center'>".$row->quantity."</td><td align='center'>0</td>";
				}else{
					$var .="<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
				}
					$var.="<td>".$row->keterangan."</td>
				</tr>";
				if($row->intid_jsatuan == 2){
					$totalQ = $totalQ + $row->quantity;
					}else{
					$totalQ2 = $totalQ2 + $row->quantity;
					}
				}	
				$var .= "<tr>
					<td colspan ='2' align='right'>Jumlah</td>
					<td align='center'><b>".$totalQ."</b></td>
					<td align='center'><b>".$totalQ2."</b></td>
					<td>&nbsp;</td>
				</tr>";
			$var .="</table>";
			$var .="<table width='100%'>
					<tr>
						<td colspan='4'>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width='20%' align='center'>ADM GUDANG</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width='20%' align='center'>(..............................)</td>
					</tr>
				";
			$data['tampilan'] =	$var;
			$data['url'] = "";
			$this->load->view('halaman/excel',$data);
		}
		//////////check POINT////////////////////////
		function GET_SURATRETUR(){
			$var = "";
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
				
				$data['halaman']	=	"SR";
				$data['barang']	=	$this->PO->get_barang();
				
				$data['no_po']	=	$this->PO->get_nopo($data['intid_cabang'],$data['intid_week'],'SRB');
				
				$var .= '<ul style="list-style-type:none; margin:0 auto 50px -30px;">
							<li>Nomor Retur Barang
								<ul style="list-style-type:square;">
									<li style="font:Verdana, Geneva, sans-serif; font-size:18px;"><h3>'.$no_po.'</h3></li>
										</ul>
								</li>
							</ul>';
				$var .='</div><div class="demo1">
							<label for="search" id="search_label">Masukan Nama Barang </label>
							<input id="search" name="search_barang" size="50" />
							<input type="button" name="tambah" value=" + " id="btn_tambah" />
							<label id="label_verify" for="verify"></label>
						</div>';
				$var .= '
					<form method="post" action="POCO/proses_po">
					<input type="hidden" name="no_po" id="no_po" value="<?php echo $no_po;?>" readonly = "true"/>
					<input type="hidden" name="intid_week" value="<?php echo $intid_week;?>" size="2"/>
					<input type="hidden" name="intid_cabang" value="<?php echo $intid_cabang;?>" size="2" />
					<div class="temp_data"></div>
						<div class="description" style="margin:5px; font-size:10px;">
						<p><small>*</small>Barang terhapus jika quantity di-set menjadi 0 (nol)<small>*</small></p>
						</div><!-- End demo-description -->
						<div class="input-submit" style="margin:auto auto 50px auto;"></div>
					</form>
					';
				
				$data['tampilan'] = $var;
				$this->load->view('halaman/po_header',$data);				
				$this->load->view('halaman/js',$data);				
				$this->load->view('halaman/SR_hal1',$data);
			}
//line ikhlas
	/*
	* @param po_look
	* desc :  menampilkan semua po dari cabang-cabang tertentu
	*
	function po_look(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		
		$data['pagination'] = "";
		$data['po'] = $this->Po_model->get_PO_cabang($cabang[0]->intid_cabang);
		$this->load->view('halaman/po_hal2',$data);
		}
	*/
	//
	function menu_sjpusat(){
		$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
		$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
		$week = $this->Penjualan->selectWeek();
				
		$data['intid_week']	=	$week[0]->intid_week;
		$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
		$data['intid_cabang']	=	$cabang[0]->intid_cabang;
	
		$this->load->view('halaman/po_header',$data);				
		$this->load->view('halaman/SJ_hal4');	
	}
	function sj_approved(){
		$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
		$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
		$week = $this->Penjualan->selectWeek();
				
		$data['intid_week']	=	$week[0]->intid_week;
		$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
		$data['intid_cabang']	=	$cabang[0]->intid_cabang;
	
		$query = $this->PO->OpenDB('select * from spkb where spkb.terkirim = 0 
								   	and spkb.intid_cabang = "'.$data['intid_cabang'].'" 
									and spkb.no_sj != "" order by week desc');
		$data['query']	= $query;
		$this->load->view('halaman/po_header',$data);				
		$this->load->view('halaman/SJ_hal5',$data);
		}
	function sj_accept(){
			$no = $this->input->get('no');
			$this->db->query('update spkb set terkirim = 1, tgl_terima = "'.date('Y-m-d H:i:s').'" 
																					 where no_sj = "'.$no.'"');
			$this->session->set_flashdata('messages', '<script>window.alert("STOK SUDAH BERTAMBAH")</script>');
			redirect('POCO/sj_approved');
		}
	function print_stock_cab(){
		$this->load->model('Cabang_model');
		$intid_week = $this->input->post('intid_week');
		$intid_cabang = $this->input->post('intid_cabang');
		
		$data['id_cabang'] = $intid_cabang;
		$data['intid_week'] = $intid_week;
		$data['user'] = $this->session->userdata('username');
		$nm_cabang = $this->Cabang_model->select($intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
           if($this->input->post('intid_week') == "START"){ 
				$data['query']  =  $this->PO->getLap_Stok_cab_START($intid_cabang,$intid_week);
				}else{
				//$data['query']  =  $this->PO->getLap_Stok_cab_3($intid_cabang,$intid_week);
				$data['query']  =  $this->PO->getLap_Stok_cab_4($intid_cabang,$intid_week);
			}
		//$this->load->view('halaman/ST_hal4',$data);
		//$this->load->view('halaman/ST_hal3',$data);
		}
	function rekursif_stok_barang(){
		
	}
		//look all po
//line ikhlas
	/*
	* @param po_look
	* desc :  menampilkan semua po dari cabang-cabang tertentu
	*/
	function po_look2(){
		$cabang = $this->User->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang->select($cabang[0]->intid_cabang);
		
		$data['pagination'] = "";
		$data['po'] = $this->PO->get_PO_cabang($cabang[0]->intid_cabang);
		$this->load->view('halaman/po_hal3',$data);
	}
	function sjbiasa(){
		$this->load->view('halaman/SJ_hal6');
 	}
	function sjbuat(){
		$cabang	=	$this->User->getCabang($this->input->post('intid_cabang'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
				
				$data['halaman']	=	"po";
				$data['barang']	=	$this->PO->get_barang();
				
				$data['no_po']	=	$this->PO->get_nopo($data['intid_cabang'],$data['intid_week'],'POPUSAT');
				$this->load->view('halaman/po_header',$data);				
				$this->load->view('halaman/js',$data);				
				$this->load->view('halaman/po_hal1',$data);				
			
 	}
	
	function DARURAT($intid_po){
		//$no_po = "3033/29/PO/07/2013";
		//$query = $this->db->query('select * from po_detail where intid_po = "'.$intid_po.'"');
		$query = $this->db->query('select surat_jalan.* from surat_jalan where intid_sj = '.$intid_po.'');
		foreach($query->result() as $row){
		//echo "yeah".$row->intid_po."<br />";
			//$query2 = $this->db->query('select * from po_detail where po_detail.intid_po = '.$row->intid_po.'');
			//foreach($query2->result() as $rok){
				//echo 'insert ignore into po_detail_ values("'.$row->no_po.'","'.$rok->intid_barang.'","'.$rok->qty.'","1","'.$rok->ket.'");<br />';
			//}
		//echo "insert ignore into po_ values('".$row->no_po."','".$row->intid_cabang."',".$row->intid_week.",'','".$row->no_spkb."',".$row->is_sj.");<br />";		
		//echo "insert ignore into spkb values('".$row->no_spkb."','".$row->no_sj."','','".$row->tgl_order."','".$row->tgl_kirim."','".$row->via."','".$row->intid_week."','".$row->intid_cabang."','','');<br />";
		$query2 = $this->db->query('select * from surat_jalan_detail where surat_jalan_detail.intid_sj = '.$row->intid_sj.'');
			foreach($query2->result() as $rok){
				echo 'insert ignore into spkb_detail values("'.$row->no_spkb.'","'.$rok->intid_barang.'","'.$rok->qty.'","'.$rok->ket.'");<br />';
			}
		
		}
	}
	function returAdd(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			$var = "";	
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
				
				$data['halaman']	=	"po";
				$data['barang']	=	$this->PO->get_barang();
				
				$data['no_srb']	=	$this->PO->get_nopo($data['intid_cabang'],$data['intid_week'],'SRB');
				
				$var .= '<h2 class="title">BUAT SURAT RETUR BARANG</h2>
                    <div class="entry"><div>
					<ul style="list-style-type:none; margin:0 auto 50px -30px;">
						<li>Nomor Surat Retur Barang
							<ul style="list-style-type:square;">
								<li style="font:Verdana, Geneva, sans-serif; font-size:18px;"><h3>'.$data['no_srb'].'</h3></li>
								</ul>
						</li>
					</ul>
					</div>
				<div class="demo1">
					<label for="search" id="search_label">Masukan Nama Barang </label>
					<input id="search" name="search_barang" size="50" />
					<input type="button" name="tambah" value=" + " id="btn_tambah" />
					<label id="label_verify" for="verify"></label>
				</div><!-- End demo -->
				<form method="post" action="'.base_url().'POCO/Proses_Retur">
				<input type="hidden" name="no_srb" id="no_po" value="'.$data['no_srb'].'" readonly = "true"/>
				<input type="hidden" name="intid_week" value="'.$data['intid_week'].'" size="2"/>
				<input type="hidden" name="intid_cabang" value="'.$data['intid_cabang'].'" size="2" />
				<div class="temp_data"></div>
					<div class="description" style="margin:5px; font-size:10px;">
					<p><small>*</small>Barang terhapus jika quantity di-set menjadi 0 (nol)<small>*</small></p>
					</div><!-- End demo-description -->
					<div class="input-submit" style="margin:auto auto 50px auto;"></div>
				</form></div>';
				$data['tampilkan'] = $var;
				$this->load->view('halaman/po_header',$data);				
				$this->load->view('halaman/js',$data);				
				$this->load->view('halaman/po_hal4',$data);				
		}
	function Proses_Retur(){
			if($_POST){
				$data = array('no_srb' => $this->input->post('no_srb'),
								'intid_cabang' => $this->input->post('intid_cabang'),
								'intid_week' => $this->input->post('intid_week'),
								);
				$this->db->trans_start();
				$this->PO->insert_DataRetur(1,$data);
				$this->db->trans_complete();
				
				$barang = $this->input->post('barang');
				for($i=0;$i<sizeof($barang);$i++){
					if(isset($barang[$i]['intquantity']) and $barang[$i]['intquantity'] != 0){
						$data = array('no_srb' => $barang[$i]['no_po'],
										'intid_barang' => $barang[$i]['intid_barang'],
										'quantity' => $barang[$i]['intquantity'],
										'status'	=> 1,
										'keterangan' => $barang[$i]['keterangan']);
						$this->PO->insert_DataRetur(2,$data);
						$this->db->trans_complete();
						}
					}
				if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('POCO/returAdd');
					}else{
					$this->db->trans_commit();	
					redirect('POCO/GET_RETUR/?no='.$this->input->post('no_srb'));
					}
				}else{
					redirect('home');
					}
		}
	function GET_RETUR(){
			$no_srb	= $this->input->get('no');
			if($no_srb !=''){
				$query	=	$this->PO->OpenDB('select pod.*,
									  b.strnama,
									  (select strnama_cabang 
									   	from cabang c inner join retur_ po on po.intid_cabang = c.intid_cabang where po.no_srb = pod.no_srb group by c.intid_cabang) strnama_cabang,
									  (select datetgl 
									   	from  retur_ re where re.no_srb = pod.no_srb group by re.no_srb) timeNow,
									  b.intid_jsatuan
									  from retur_detail_ pod inner join barang b on b.intid_barang = pod.intid_barang  
									  where pod.no_srb = "'.$no_srb.'" order by b.strnama asc');
				$query = $query->result();
				}else{
					$query = $this->PO->DataPO();
				}
			
			$var = "";
			$var .='
			<img src="'.base_url().'images/logo.jpg" align="left" />
				<div style="display:block; height:80px;" align="right">
					 <a href="javascript:window.print()" onclick=location.href="'.base_url().'/po" />
					 <img align="right" src="'.base_url().'/images/print.jpg"/></a><h1 align="center">SURAT RETUR BARANG</h1>
			</div>
				<br />
				<table>
				<tr>
					<td>TANGGAL</td><td>:</td><td>'.date('d - m - Y, H:i:s',strtotime($query[0]->timeNow)).'</td></tr>
					<tr>
					<td>CABANG</td><td>:</td><td>'.strtoupper($query[0]->strnama_cabang).'</td></tr>
					<tr>
					<td><h3>NO SRB</h3></td><td> :</td><td><h3><b>'.$query[0]->no_srb.'</b></h3></td>
					</tr>
				</table>
				<br  />
				<table border="1" width="100%">
				<tr>
					<th rowspan="2">NO.</th>
					<th rowspan="2">NAMA BARANG</th>
					<th colspan="2">JUMLAH</th>
					<th rowspan="2">KETERANGAN</th>
				</tr>
				<tr>
					<th>PCS</th>
					<th>SET</th>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>';
					$count_Totalpiece = 0;
					$count_Totalset = 0;
					$no = 1;
			foreach($query as $row){
				if($row->intid_jsatuan == 2){
					$count_Totalpiece = $count_Totalpiece + $row->quantity;
				}else{
					$count_Totalset	=	$count_Totalset + $row->quantity;
					}
				$var .='<tr style="padding:5px;"><td align="center">'.$no++.'</td><td>'.$row->strnama.'</td>';
				if($row->intid_jsatuan == 2){
					$var .='<td align="center">'.$row->quantity.'</td><td align="center">0</td>';
				}else{
						$var .= '<td align="center">0</td><td align="center">'.$row->quantity.'</td>';
				}
				$var .= '<td>'.$row->keterangan.'</td>';
				$var .= '</tr>';
				}
				$var .= '<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td colspan="2" align="right"><b>JUMLAH</b></td>
					<td align="center"><b>'.$count_Totalpiece.'</b></td>
					<td align="center"><b>'.$count_Totalset.'</b></td>
					<td>&nbsp;</td>
				</tr>
				</table>
				<table width="100%">
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td width="20%">&nbsp;</td>
					<td width="20%">&nbsp;</td>
					<td width="20%">&nbsp;</td>
					<td width="40%" align="center">ADM.CABANG</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td align="center">( ........................ )</td>
				</tr>
				</table>';
			$data['tampilkan'] =	$var;
			$data['url'] = "";//base_url()."POCO/GET_EXCEL_SPKB/?no=".$no_spkb."";
			$this->load->view('halaman/SR_hal2',$data);
			}
	function STTB_INSERT(){
			$week = $this->Penjualan->selectWeek();
			$var = "";
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			$temp = "";	
			$intid_week	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$intid_cabang	=	$cabang[0]->intid_cabang;
				
			$query = $this->PO->openDB('select *,rt.datetgl waktu,(select strnama_cabang from cabang where intid_cabang = rt.intid_cabang )strnama_cabang 
																from retur_ rt where 
																	 rt.is_sttb = 0 and (rt.no_sttb is NULL or rt.no_sttb = "") order by waktu desc LIMIT 0, 10');
			$data['intid_week']	=	$week[0]->intid_week;			
			$var .= '<div id="page">
						<div id="page-bgtop">
							<div id="content">
								<div>	<h2 class="title">BUAT STTB dari SRB</h2>
									<div class="entry">                     
					<div>
				
				<label>Masukan Cabang</label>
				<input type="text" name="cabang" id="cabang" class="cabang" />
				<input type="button" name="spkb_button" id="spkb_button" class="spkb_button" value="search" />
				<div id="result">';
			$var .= "<form method='post' action='".base_url()."POCO/STTB'>";
					$var .= "<br/>
					<table border='1' width='100%' style='background-color:#FFF'>";
					$var .= "<tr>
						<th width='25%'>Cabang</th>
						<th width='35'>Week</th>
						<th width='25%'>Nomor SRB</th>
						<th width='35'>Waktu PO</th>
						<th>Action</th>
						</tr>";
					foreach($query->result() as $rok){
						$temp .= "<tr>
							<td align='center'>".$rok->strnama_cabang."</td>
							<td align='center'>".$rok->intid_week."</td>
							<td align='right'>".$rok->no_srb."</td>
							<td align='center'>".$rok->waktu."</td>
							<td><a href='".base_url()."POCO/get_PO/?no=".$rok->no_srb."'>VIEW</a></td>
							</tr>";
						}
						if($temp != ""){
							$var .= $temp;
							//$var .= "<tr><td colspan='5' align='right'><input type='submit' name='submit' value='LIHAT PO' /></td></tr>";
					}else{
								$var .= "<tr><td colspan='5'>TIDAK ADA SURAT RETUR!</td></tr>";
								}
					$var .= "</table></form>";
					$var .= '</div></body>
								<script type="text/javascript">   
									$(".spkb_button").click(function(){
												var json_data = {
														strnama_cabang	:	$(".cabang").val(),
														intid_week	:'.$intid_week.',
														ajax : 1
														}
																$.ajax({
																		   url	:	"'.base_url().'POCO/FORM_STTB_RESULT",
																		   type : "POST",
																		   data : json_data,
																		   success:function(data){
																			   $("#result").html(data);
																			   },
																		   }); 
																	});
									</script>
								</html></div></div></div></div>';
			
			$data['tampilkan'] = $var;
			
			$this->load->view('halaman/po_header',$data);
			//$this->load->view('halaman/method_spkb',$data);
			$this->load->view('halaman/STTB_hal1',$data);
		}
	//AJAX CALL
	function FORM_STTB_RESULT(){
			$var = "Akses Dilarang!! Contact Admin Pusat!";
			$temp = "";
			$ajax	=	$this->input->post('ajax');
			$strnama_cabang = $this->input->post('strnama_cabang');
			$intid_week = $this->input->post('intid_week');
			//Ajax Time 
			if($ajax == 1){
				$cabang	=	$this->User->getCabang($strnama_cabang);			
				$intid_cabang = $cabang[0]->intid_cabang;
				$query = $this->PO->openDB('select *,rt.datetgl waktu,(select strnama_cabang from cabang where intid_cabang = '.$intid_cabang.')strnama_cabang from retur_ rt where rt.intid_cabang = '.$intid_cabang.' 
																	and rt.is_sttb = 0 and (rt.no_sttb is NULL or rt.no_sttb = "")order by waktu ASC');
					$var = "<form method='post' action='".base_url()."POCO/STTB'>";
					$var .= "<h2>SRB ".$strnama_cabang.", week : ".$intid_week."</h2><table border='1' width='100%' style='background-color:#FFF'>";
					$var .= "<tr>
						<th width='10%'>Pilih</th>
						<th width='25%'>Cabang</th>
						<th width='35'>Week</th>
						<th width='25%'>Nomor SRB</th>
						<th width='35'>Waktu SRB</th>
						<th>Action</th>
						</tr>";
					foreach($query->result() as $rok){
						$temp .= "<tr>
							<td align='center'><input type='checkbox' name='RT[]' value='".$rok->no_srb."' /></td>
							<td align='center'>".$rok->strnama_cabang."</td>
							<td align='center'>".$rok->intid_week."</td>
							<td align='right'>".$rok->no_srb."</td>
							<td align='center'>".$rok->waktu."</td>
							<td><a href='".base_url()."POCO/GET_RETUR/?no=".$rok->no_srb."'>VIEW</a></td>
							</tr>";
						}
						if($temp != ""){
							$var .= $temp;
							$var .= "<tr><td colspan='6' align='right'><input type='submit' name='submit' value='LIHAT PO' /></td></tr>";
					}else{
								$var .= "<tr><td colspan='6'>TIDAK ADA SURAT RETUR!</td></tr>";
								}
					$var .= "</table></form>";
					echo $var;
				}else{
					echo $var;
				}
			}
	function STTB(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
				
			$intid_week	=	$week[0]->intid_week;
			$strnama_cabang	=	$nama_cabang[0]->strnama_cabang;
			
			$var = "";
			$arr	= array();
			$data['input_type']	= "";
			$rt		= $this->input->post('RT');
			$no_srb	=	array();
			$query0 = $this->PO->OpenDB('select intid_cabang, (select strnama_cabang from cabang where intid_cabang = rt.intid_cabang) strnama_cabang from retur_ rt where rt.no_srb = "'.$rt[0].'"');
			$rock= $query0->result();
			
			$no_STTB	=	$this->PO->get_nopo($rock[0]->intid_cabang,$intid_week,'STTB');
			$strnama_cabang = $rock[0]->strnama_cabang;
			$intid_cabang = $rock[0]->intid_cabang;
			
			for($i=0;$i<sizeof($rt);$i++){
				$data['input_type']	.=	"<input type='hidden' name='RT[".$i."]' value='".$rt[$i]."' />";
				if($rt[$i] != ''){
					$query = $this->db->query('select pod.*, b.strnama, b.intid_jsatuan from retur_detail_ pod inner join barang b on b.intid_barang = pod.intid_barang where pod.no_srb = "'.$rt[$i].'" order by b.strnama asc');
					foreach($query->result() as $row){
							$arr[] = array('intid_barang' => $row->intid_barang,
										   'strnama'	=>$row->strnama,
										   'quantity'	=> $row->quantity,
										   'intid_jsatuan' => $row->intid_jsatuan,
										   'keterangan' => $row->keterangan,
										   'no_srb' => $rt[$i]);
						}
					}
				}
				//$data['arr']	=	$arr;
				//$data['rt']	=	$rt;
			
			$var .= '<body>
<div align="right" style="margin-right:80px;"><h3 style="float:right;"></h3></div>
<div style="height:80px;"><img src="'.base_url().'images/logo.jpg" align="left" /></div>';
		$susun = array();
			//susun barang dengan semua jumlah yang ad po-nya
			for($k=0;$k<sizeof($arr);$k++){
					//masukan quantity sesuai dengan intid_barang
						//lakukan pencarian 
						$temp = 0;
						for($i=0;$i<sizeof($susun);$i++){
							if($susun[$i]['intid_barang'] == $arr[$k]['intid_barang']){
								$susun[$i]['quantity'] = $susun[$i]['quantity'] + $arr[$k]['quantity'];
								$susun[$i]['keterangan'] .=  $arr[$k]['keterangan'].", ";
								$temp = 1;
								}
							}
						//jika tidak ditemukan pencarian, masukan sebagai data abaru	
						if($temp == 0){
							$susun[]	= array('intid_barang' => $arr[$k]['intid_barang'],'strnama' => $arr[$k]['strnama'],'quantity' => $arr[$k]['quantity'],'intid_jsatuan' => $arr[$k]['intid_jsatuan'],'keterangan' => $arr[$k]['keterangan']);
							}
						
					}		
			//sekarang menampilkan semua data yang disusun
			for($i=0;$i<sizeof($susun);$i++){
				//echo "".$susun[$i]['intid_barang'].", ".$susun[$i]['strnama']." ,".$susun[$i]['quantity']."<br />";
				}	
			$var .= '<div style="float:right; margin-right:60px;"></div>
			<form method="post" action="'.base_url().'POCO/Proses_STTB">
			<input type="hidden" name="intid_cabang" value="'.$intid_cabang.'" />
			<table>
			<tr><td>TANGGAL </td><td>:</td><td>'.date('d-m-Y').'</td></tr>
			<tr><td>CABANG</td><td>:</td><td>'.strtoupper($strnama_cabang).'</td></tr>';
				for($i=0;$i<sizeof($rt);$i++){
					$var .= "<tr><td>NO. SRB</td><td>:</td><td style='font-size:24px;'>".$rt[$i]."</td></tr>";
					}
			$var .= '</table>
				<table width="100%" border="1">
				<tr>
					<th rowspan="2">No.</th>
					<th rowspan="2">Nama Barang</th>
					<th colspan="2">JUMLAH</th>
					<th rowspan="2">Keterangan</th>
					</tr>
				   <tr>
						<th>PCS</th>
						<th>SET</th>
				   </tr>';
    $no = 1;
	$pieces = 0;
	$set = 0;
	//sekarang menampilkan semua data yang disusun
	for($i=0;$i<sizeof($susun);$i++){
		$var .= "<tr>
			<td align='center'>".$no++."</td>
			<td><input type='text' name='namaBarang[".$i."]' value='".$susun[$i]['strnama']."' size='50' disabled /><input type='hidden' name='intidBarang[".$i."]' value='".$susun[$i]['intid_barang']."' /></td>";
		if($susun[$i]['intid_jsatuan'] == 2){
			$pieces = $pieces +$susun[$i]['quantity'];
			$var .= "<td align='center'><input type='text' name='quantity[".$i."]' value='".$susun[$i]['quantity']."' size='3' readonly /></td><td align='center'>0</td>";
		}else{
			$set = $set + $susun[$i]['quantity'];
			$var .= "<td align='center'>0</td><td align='center'><input type='text' name='quantity[".$i."]' value='".$susun[$i]['quantity']."' size='3' readonly /></td>";
			}
		$var .= "<td><input type='text' name='keterangan[".$i."]' value='".$susun[$i]['keterangan']."' size='20' readonly /></td>
			</tr>";
		}
		$var .='
			<tr>
				<td align="right" colspan="2">JUMLAH</td>
				<td align="center">'.$pieces.'</td>
				<td align="center">'.$set.'</td>
				<td></td>
			</tr>
			</table>
<input type="hidden" name="STTB" value="'.$no_STTB.'" />
<input type="hidden" name="intid_week" value="'.$intid_week.'" />
'.$data['input_type'].'
<br /><div align="right">
<input type="submit" name="submit" value="Cetak STTB"/>
</div>
</form>';
			
			$data['tampilkan'] = $var;
			
			$this->load->view('halaman/head',$data);
			$this->load->view('halaman/STTB_hal2',$data);
		}
	function Proses_STTB(){
			$no_sttb = $this->input->post('STTB');
			$rt	=	$this->input->post('RT');
			$intid_cabang = $this->input->post('intid_cabang');
			$week = $this->input->post('intid_week');
			$tgl_order = "";
			
			$namaBarang	= $this->input->post('namaBarang');
			$intidBarang= $this->input->post('intidBarang');
			$quantity	= $this->input->post('quantity');
			$keterangan	= $this->input->post('keterangan');
			
			$this->db->trans_start();
			$this->db->query('insert into sttb values("'.$no_sttb.'", "", "'.date('Y-m-d H:i:s').'","'.$tgl_order.'","","","'.$week.'","'.$intid_cabang.'","","")');
			$this->db->trans_complete();
			for($i=0;$i<sizeof($intidBarang);$i++){
				$this->db->query('insert into sttb_detail values ("'.$no_sttb.'", "'.$intidBarang[$i].'","'.$quantity[$i].'","'.$keterangan[$i].'")');
				$this->db->trans_complete();
				}
			
			for($i=0;$i<sizeof($rt);$i++){
				$select = 'update retur_ set no_sttb="'.$no_sttb.'",is_sttb = 1 where no_srb = "'.$rt[$i].'"';
				$this->db->trans_complete();
				$this->db->query($select);
				}
			if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('POCO/STTB');
				}else{
					$this->db->trans_commit();	
					redirect('POCO/GET_STTB/?no='.$no_sttb.'');
				}
		}
	function GET_STTB(){
		$no_sttb	= $this->input->get('no');
			$var	=	"";
			$query	= $this->PO->openDB('select spb.*,b.strnama,b.intid_jsatuan from sttb_detail spb inner join barang b on b.intid_barang = spb.intid_barang where spb.no_sttb = "'.$no_sttb.'" and spb.quantity != 0 order by b.strnama asc');
			$query2	= $this->PO->openDB('select rt.no_srb,sttb.no_sttb,(select strnama_cabang from cabang where cabang.intid_cabang = sttb.intid_cabang)strnama_cabang, sttb.waktu from sttb left join retur_ rt on sttb.no_sttb = rt.no_sttb where sttb.no_sttb like "'.$no_sttb.'"');
			$rok = $query2->result();
			$temp = "";
			/*
			if(isset($rok[0]->waktu)){
				$temp = date('d - m - Y',strtotime($rok[0]->waktu));
			}else{
				$temp = "00 - 00 - 0000";
			}
			if(isset($rok[0]->strnama_cabang)){
				$temp2 = strtoupper($rok[0]->strnama_cabang);
			}else{
				$temp2 = "--tidak ada cabang --";
			}
			*/
			if(isset($rok[0]->waktu)){
				$temp = date('d - m - Y',strtotime($rok[0]->waktu));
				}else{
					$temp = "0000-00-00";
				}
				if(isset($rok[0]->strnama_cabang)){
					$temp2 = strtoupper($rok[0]->strnama_cabang);
				}else{
					$temp2 = "";
				}
				$var .= "<table>
			<tr><td>BANDUNG</td><td>,</td><td>".$temp."</td></tr>
			<tr><td><b>CABANG</b></td><td>:</td><td><b>".$temp2."</b></td></tr>
			";
			foreach($query2->result() as $rok){
				$var .= "<tr><td><b>NO SRB</b></td><td>:</td><td><b>".$rok->no_srb."</b></td></tr>";
				}
			
			$var .= "<tr><td><b>NO STTB</b></td><td>:</td><td><b style='font-size:20px;'>".$no_sttb."</b></td></tr>";
			$var .="</table>";
			$var .= "<table border='1' width='100%'>";
			$var .="<tr>
					<th rowspan='2'>NO.</th>
					<th rowspan='2'>NAMA BARANG</th>
					<th colspan='2'>JUMLAH</th>
					<th rowspan='2'>KETERANGAN</th>
				</tr>";
			$var .="<tr>
					<th>PCS</th>
					<th>SET</th>
					</tr>";
			$no = 1;
			$total =0;
			$totalQ = 0;
			$totalQ2 = 0;
			foreach($query->result() as $row){
				$var	.=	"<tr>
					<td align='center'>".$no++."</td>
					<td>".$row->strnama."</td>";
				if($row->intid_jsatuan == 2){
					$var .="<td align='center'>".$row->quantity."</td><td align='center'>0</td>";
				}else{
					$var .="<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
				}
					$var.="<td>".strtoupper($row->keterangan)."</td>
				</tr>";
				if($row->intid_jsatuan == 2){
					$totalQ = $totalQ + $row->quantity;
					}else{
					$totalQ2 = $totalQ2 + $row->quantity;
					}
				}	
				$var .= "<tr>
					<td colspan ='2' align='right'>JUMLAH</td>
					<td align='center'><b>".$totalQ."</b></td>
					<td align='center'><b>".$totalQ2."</b></td>
					<td>&nbsp;</td>
				</tr>";
			$var .="</table>";
			$var .="<table width='100%'>
					<tr>
						<td colspan='4'>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width='20%' align='center'>ADM GUDANG</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
					</tr>
					<tr>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td>&nbsp;</td>
						<td width='20%' align='center'>(..............................)</td>
					</tr>
				";
			$data['tampilan'] =	$var;
			$data['url'] = base_url()."POCO/GET_EXCEL_STTB/?no=".$no_sttb."";
			$this->load->view('halaman/print',$data);
			}
	function GET_EXCEL_STTB(){
			}
	function SJ2_INSERT(){
		$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			$var = "";	
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$hal = $this->input->get('per_page');
			if($hal == "" or !isset($hal)){
				$hal = 0;
			}
			$limit = 15;
				$query = $this->PO->STTB_SJ($hal,$limit);
				$query1 = $this->PO->OpenDB('select count(*) total from spkb inner join cabang on cabang.intid_cabang = spkb.intid_cabang 
														where spkb.no_sj = ""');
					$result = $query1->result();
					$total = $result[0]->total;
					
				$this->load->library('pagination');
				$config['base_url']  = base_url()."POCO/SJ2_INSERT/?cabang=0";
				$config['total_rows']	=	$total;
				$config['per_page'] 	=	$limit;
				$config['num_links']	=	6;
				$config['first_link']	=	"awal ";
				$config['last_link']	=	"akhir";
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$pagination = $this->pagination->create_links();
					
				$query	=	$this->PO->STTB_SJ($hal,$limit);
				
				 $var .= '<div id="page">
									<div id="page-bgtop">
										<div id="content">
											<div>	<h2 class="title">SURAT JALAN</h2>
												<div class="entry">
							<div style="display:block; width:100%;height:30px;margin:5px auto 5px auto;">
								<form method="POST" action="'.base_url().'POCO/SJ2_INSERT_SEARCH">
									<label>masukan nama cabang :</label><input type="text" name="cabang" id="cabang" /><input type="submit" name="bttnCabang" value="cari" />
									</form>
							</div>';
				if(isset($pagination)){
					 $var .= "<div align='center' style='margin:10px auto 10px auto; display:block;'>".$pagination."</div>";
				}else{
					$var .= "<small>pagination tidak aktif</small><br />";
				}
				$var .= '<div id="result"><table border="1" width="100%" style="font:Verdana, Geneva, sans-serif; font-size:14px; background-color:#FFF;">
							<tr>
								<th>Waktu</th>
								<th>Cabang</th>
								<th>Surat Tanda Terima Barang</th>
								<th>Perintah Cetak Sj</th>
							</tr>';
					foreach($query->result() as $row){
						$var .= "<tr>";
						$var .= "<td>".$row->waktu."</td>
							<td>".$row->strnama_cabang	."</td>
							<td> <a href='".base_url()."POCO/GET_STTB/?no=".$row->no_sttb."' />".$row->no_sttb."</a></td>
							<td align='center'><a href='".base_url()."POCO/Proses_SJ2/?no=".$row->no_sttb."'>CETAK</a> </td>";
						$var .= "</tr>";
						}
				$var .='</table></div></div></div></div>';
							$data['tampilkan'] = $var;
							$this->load->view('halaman/po_header');
							$this->load->view('halaman/STTB_hal1',$data);
			}
	function Proses_SJ2(){
				$no_sttb	=	$this->input->get('no');
				$data['no_sttb'] = $no_sttb;
				$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
				$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
				$week = $this->Penjualan->selectWeek();
				
				$data['halaman'] = 'SJ';	
				$data['barang']	=	$this->PO->get_barang();
					
				$data['intid_week']	=	$week[0]->intid_week;
				$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
				
				$query = $this->PO->OpenDB('select intid_cabang, (select strnama_cabang from cabang where cabang.intid_cabang = sttb.intid_cabang) strnama_cabang,
																  (select waktu from retur_ rt where rt.no_sttb = sttb.no_sttb limit 0,1)tgl_rt from sttb where no_sttb = "'.$data['no_sttb'].'"');
				$result = $query->result();
				
				$intid_cabang	=	$result[0]->intid_cabang;
				$strnama_cabang_spkb = $result[0]->strnama_cabang;
				$waktu = date('Y-m-d',strtotime($result[0]->tgl_rt));
					
				$no_po	=	$this->PO->get_nopo($result[0]->intid_cabang,$data['intid_week'],'SJ');
				$query	=	$this->PO->OpenDB('select spd.*,barang.strnama,barang.intid_jsatuan 
														  from sttb_detail spd inner join barang on barang.intid_barang = spd.intid_barang 
														  where spd.no_sttb="'.$data['no_sttb'].'" and spd.quantity != 0 order by barang.strnama asc');	
				$var = "";
				$var .= '<div id="page">
							<div id="page-bgtop">
								<div id="content">
									<div>	<h2 class="title">Pembuatan Surat Jalan</h2>
										<div class="entry">                     
						<div>
							<form method="post" action="'.base_url().'POCO/Proses_SJ_STEP3">
					<ul style="list-style-type:none; margin:0 0 0 350px;">
					<li style="margin:auto auto auto -380px;">
						CAB/SC	: '. $strnama_cabang_spkb.'
						<h3>NO Surat Jalan : '.$no_po.'<input type="hidden" size="2" name="no_sj" value="'.$no_po.'" /><input type="hidden" size="2" name="no_sttb" value="'. $no_sttb.'" /></h3>
						</li>
					</ul>
					<table style="float:right;">
						<tr>
							<td>Bandung</td><td>,</td><td>'.date('d - m - Y').'</td>
						</tr>
						<tr>
							<td>TANGGAL RETUR</td><td>:</td><td><input type="text" name="tgl_order" id="demo3" size="15" value="'.$waktu.'" readonly /></td>
						</tr>
						<tr>
							<td>TANGGAL SJ</td><td>:</td><td><input type="text" name="tgl_kirim" id="demo4" size="15" /><a href=javascript:NewCssCal("demo4","yyyymmdd") > <img src="'.base_url().'images/cal.gif" width="16" height="16" alt="Pick a date" /></a></td>
						</tr>
						<tr>
							<td>PENGIRIMAN VIA</td><td>:</td><td><input type="text" name="via" size="15" /></td>
						</tr>
					</table>
					<table border="1" width="100%" style="background-color:#FFF;">
					<tr>
						<th width="3" rowspan="2">No.</th>
						<th width="30%" rowspan="2">Nama Barang</th>
						<th width="20%" colspan="2">JUMLAH</th>
						<th width="7%" rowspan="2">Quantity Terkirim</th>
						<th width="40%" rowspan="2">Keterangan</th>
					</tr>
					<tr>
						<th>PCS</th>
						<th>SET</th>
					</tr>';
	$i = 0;
	$no = $i+1; 
	$pieces = 0;
	$set = 0;
	foreach($query->result() as $row){
		$var .= "<tr>
			<td align='center'>".$no++."<input type='hidden' name='intid[".$i."]' value='".$row->intid_barang."' size='2' />
			</td>
			<td><input type='text' name='namaBarang[".$i."]' value='".$row->strnama."' size='30' disabled /></td>";
		if($row->intid_jsatuan == 2){
			$pieces = $pieces + $row->quantity;
		$var .= "<td align='center'>".$row->quantity."</td>
			<td align ='center'>0</td>";
		}else{
			$set = $set + $row->quantity;
		$var .= "<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
			}
		$var .= "<td><input type='text' name='quantity[".$i."]' value='".$row->quantity."' size='2' /></td>
			<td><input type='text' name='keterangan[".$i."]' value='".$row->keterangan."' size='30' />
			</td>
		</tr>";
		$i++;
		}
		$var .= "<tr id='content-list'><td colspan='6' align='right'>
		<input type='submit' name='submit' value='CETAK Surat Jalan' style='margin:10px;' />
		</td></tr>";
		$var .='</table>
				<input type="hidden" id="list-data" value="'.$i.'"/>
				<div class="demo" style="margin:20px;">
				<div style="float:left;">CARI BARANG : </div> 
					<input id="search" name="search_barang" size="50" />
					<input type="button" name="tambah" value=" + " id="btn_tambah" />
					<label id="label_verify" for="verify"></label>
				</div><!-- End demo -->
				<div class="temp_data"></div>
					<div class="description">
					<p> <small>*</small> Barang terhapus jika quantity di-set menjadi 0 (nol)<small>*</small></p>
					</div><!-- End demo-description -->
				</form>
				
									  </div></div></div></div>';
					$data['tampilkan']	=	$var;
					$this->load->view('halaman/po_header');
					$this->load->view('halaman/js',$data);
					$this->load->view('halaman/STTB_hal1',$data);
		}
	function Proses_SJ_STEP3(){
		if($_POST){
				$no_sj	=	$this->input->post('no_sj');
				$no_sttb	=	$this->input->post('no_sttb');
				$namaBarang	=	$this->input->post('namaBarang');
				$intid	=	$this->input->post('intid');
				$quantity	=	$this->input->post('quantity');
				$keterangan	=	$this->input->post('keterangan');
				$tgl_order = $this->input->post('tgl_order');
				$tgl_kirim = $this->input->post('tgl_kirim');
				$via	=	$this->input->post('via');
				
				$TnamaBarang	=	$this->input->post('TnamaBarang');
				$Tintid_barang	=	$this->input->post('Tintid_barang');
				$Tquantity	=	$this->input->post('Tquantity');
				$Tketerangan	=	$this->input->post('Tketerangan');
				
				//mulai update data table
				$this->db->trans_start();
				for($i=0; $i<sizeof($intid);$i++){
					//echo $no_spkb.",".$intid[$i].", ".$namaBarang[$i].",".$quantity[$i].",".$keterangan[$i]."<br />";
					$this->PO->OpenDB('update sttb_detail 
									 	set quantity = "'.$quantity[$i].'", keterangan = "'.$keterangan[$i].'"
										where intid_barang = "'.$intid[$i].'" and no_sttb = "'.$no_sttb.'"');
					$this->db->trans_complete();
					}
				//mulai insert
				for($i=0;$i<sizeof($Tintid_barang);$i++){
					//echo $no_spkb.",".$Tintid_barang[$i].", nama : ".$TnamaBarang[$i].",".$Tquantity[$i].",".$Tketerangan[$i]."::T<br />";
					$this->PO->OpenDB('insert into sttb_detail values("'.$no_sttb.'","'.$Tintid_barang[$i].'","'.$Tquantity[$i].'","'.$Tketerangan[$i].'")');
					$this->db->trans_complete();
					}
				//
				$this->PO->OpenDB('update sttb 
									 	set no_sj = "'.$no_sj.'", tgl_order = "'.$tgl_order.'", tgl_kirim = "'.$tgl_kirim.'", via = "'.$via.'"
										where no_sttb = "'.$no_sttb.'"');
				$this->db->trans_complete();
				if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('POCO/Proses_SJ2/?no='.$no_spkb.'');	
				}else{
					$this->db->trans_commit();	
					redirect('POCO/GET_SJ2/?no_sj='.$no_sj.'&no_sttb='.$no_sttb.'');	
				}				
			}
		}
	function GET_SJ2(){
			$data['no_sj']	=	$this->input->get('no_sj');
			$data['no_sttb']	=	$this->input->get('no_sttb');
			$query2 = $this->PO->OpenDB('select intid_cabang, 
										(select strnama_cabang from cabang where cabang.intid_cabang = sttb.intid_cabang) strnama_cabang, 
										sttb.* from sttb where sttb.no_sttb = "'.$data['no_sttb'].'"');
			$rok = $query2->result();
			$var = "";
			$var .= "<div style='margin:0 auto 0 auto;font-size:30px;background:none;width:30%;' align='center'>SURAT JALAN</b><br />
			<c style='font-size:23px;'>".strtoupper($rok[0]->strnama_cabang)."<c></div><br />";
			$var .= "<table style='float:right; background:none; margin: -20px auto 0 auto;'>
				<tr>
					<td>Bandung</td><td>,</td><td>".date('d - m - Y',strtotime($rok[0]->tgl_kirim))."</td>
				</tr>
				<tr>
					<td>WEEK</td><td>:</td><td>".$rok[0]->week."</td>
				</tr>
				<tr>
					<td>VIA</td><td>:</td><td>".strtoupper($rok[0]->via)."</td>
				</tr>
			</table>";
			$query 	=	$this->PO->OpenDB('select spk.*,b.strnama,b.intid_jsatuan from sttb_detail spk inner join barang b on b.intid_barang = spk.intid_barang where spk.no_sttb = "'.$data['no_sttb'].'" and spk.quantity != 0 order by b.strnama asc');
			$var .= '<div style="margin:50px auto auto auto;font-size:20px;">No Surat Jalan : '.$data['no_sj'].'</div>';
			$var .= '<table border="1" width="100%">';
			$var .= '<tr><th rowspan="2">NO STTB</th>
						<th rowspan="2">NAMA BARANG</th>
						<th colspan="2">JUMLAH</th>
						<th rowspan="2">KETERANGAN</th>
						</tr>';
			$var .= '<tr>
				<th>PCS</th>
						<th>SET</th>
						</tr>';
			$total_Q=0;
			$total_Q2=0;
			$var .= '<tr><td align="center">'.$data['no_sttb'].'</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></h4>';
			
			foreach($query->result() as $row){
				$var .=	"<tr>
					<td>&nbsp;</td>
					<td>".$row->strnama."</td>";
					if($row->intid_jsatuan == 2){
						$var .="<td align='center'>".$row->quantity."</td><td align='center'>0</td>";
						$total_Q = $row->quantity + $total_Q;
					}else{
						$var .="<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
						$total_Q2 = $row->quantity + $total_Q2;
					}
				$var .="<td>".strtoupper($row->keterangan)."</td>
				</tr>";
				}
			
			$var .= '<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
			$var .= '<tr>
				<th align = "center" colspan="2">TOTAL PESANAN</th>
				<th>'.$total_Q.'</th>
				<th>'.$total_Q2.'</th>
				<th>&nbsp;</th>
						</tr>';
			$var .= '</table>';
			$var .= '<table width="100%" align="center">
				<tr><td colspan="9">Barang pesanan yang tidak ada di Surat Jalan berarti kondisi barang kosong di kantor pusat.</td></tr>
				<tr><td colspan="9">Barang yang telah dipesan tidak dapat dikembalikan.</td></tr>
				<tr><td colspan="9">Klaim barang hanya kami terima 1 hari dari pengiriman barang, 1 hari dari penerimaan jarak jauh.</td></tr>
				<tr><td colspan="9">Atas perhatian dan kerjasamanya kami ucapkan terima kasih</td></tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr>
				<td>&nbsp;</td>
				<td align="center">PENERIMA</td>
				<td>&nbsp;</td>
				<td align="center">PEMERIKSA</td>
				<td>&nbsp;</td>
				<td align="center">MENGETAHUI</td>
				<td>&nbsp;</td>
				<td align="center">ADM.GUDANG</td>
				<td>&nbsp;</td>
				</tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr><td colspan="9">&nbsp;</td></tr>
				<tr>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				<td align="center">(    .................................     )</td>
				<td>&nbsp;</td>
				</tr>
			</table>';
			$data['tampilan'] =	$var;
			$data['url'] = base_url().'POCO/GET_EXCEL_SJ/?no_sj='.$data['no_sj'].'&no_sttb='.$data['no_sttb'].'';
			$this->load->view('halaman/print',$data);
			}
	 function SJ2_INSERT_SEARCH(){
			$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
			$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
			$week = $this->Penjualan->selectWeek();
			$data['intid_week']	=	$week[0]->intid_week;
			$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
			$data['intid_cabang']	=	$cabang[0]->intid_cabang;
			$limit = 15;
				
				if($_POST){							
					$varcabang = $this->input->post('cabang');
					$hal	=	0;
				}else{
					$varcabang = $this->input->get('cabang');
					$hal	=	$this->input->get('per_page');
					if($hal == null){
						$hal = 0;
					}
				}
					$query = $this->PO->STTB_SJ_SEARCH($varcabang,$hal,$limit);

					$query2 = $this->PO->OpenDB('select count(*) total from sttb inner join cabang on cabang.intid_cabang = spkb.intid_cabang 
														where cabang.strnama_cabang like "'.$varcabang.'" and (sttb.no_sj = "" or sttb.no_sj is NULL)');
					$result = $query2->result();
					$total = $result[0]->total;
					
				$this->load->library('pagination');
				$config['base_url']  = base_url()."POCO/SJ2_INSERT_SEARCH/?cabang=".$varcabang."";
				$config['total_rows']	=	$total;
				$config['per_page'] 	=	$limit;
				$config['num_links']	=	6;
				$config['first_link']	=	"awal ";
				$config['last_link']	=	"akhir";
				$config['page_query_string'] = TRUE;
				$this->pagination->initialize($config);
				$pagination = $this->pagination->create_links();
				
				$var .= '<div id="page">
									<div id="page-bgtop">
										<div id="content">
											<div>	<h2 class="title">SURAT JALAN</h2>
												<div class="entry">
							<div style="display:block; width:100%;height:30px;margin:5px auto 5px auto;">
								<form method="POST" action="'.base_url().'POCO/SJ2_INSERT_SEARCH">
									<label>masukan nama cabang :</label><input type="text" name="cabang" id="cabang" /><input type="submit" name="bttnCabang" value="cari" />
									</form>
							</div>';
				if(isset($pagination)){
					 $var .= "<div align='center' style='margin:10px auto 10px auto; display:block;'>".$pagination."</div>";
				}else{
					$var .= "<small>pagination tidak aktif</small><br />";
				}
				$var .= '<div id="result"><table border="1" width="100%" style="font:Verdana, Geneva, sans-serif; font-size:14px; background-color:#FFF;">
							<tr>
								<th>Waktu</th>
								<th>Cabang</th>
								<th>Surat Tanda Terima Barang</th>
								<th>Perintah Cetak Sj</th>
							</tr>';
					foreach($query->result() as $row){
						$var .= "<tr>";
						$var .= "<td>".$row->waktu."</td>
							<td>".$row->strnama_cabang	."</td>
							<td> <a href='".base_url()."POCO/GET_STTB/?no=".$row->no_sttb."' />".$row->no_sttb."</a></td>
							<td align='center'><a href='".base_url()."POCO/Proses_SJ2/?no=".$row->no_sttb."'>CETAK</a> </td>";
						$var .= "</tr>";
						}
				$var .='</table></div></div></div></div>';
							$data['tampilkan'] = $var;
							$this->load->view('halaman/po_header');
							$this->load->view('halaman/STTB_hal1',$data);
			}
	function surat_retur() {
			$cabang = $this->User->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$limit = 10;
			$offset = $this->input->get('per_page');
			if($offset == null){
				$offset = 0;
			}
			//$query = $this->PO->get_list_data_retur($limit,$offset, $nm_cabang[0]->intid_cabang);
			$query = $this->PO->get_list_data_retur($offset, $nm_cabang[0]->intid_cabang);
			$data['po'] = $query->result();
			$this->load->library('pagination'); 
			
			$config['base_url'] = base_url()."POCO/DataSPKB/?cabang=0";
			$config['total_rows'] = $query->num_rows();
			$config['per_page'] = $limit;
			$config['num_links'] = 2;
			$config['full_tag_open'] = '<div id="pagination">';
			$config['full_tag_close'] = '</div>';
			$config['page_query_string'] = TRUE;

			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['offset']=$offset;
			$query->free_result();
		$this->load->view('halaman/SR_hal3', $data);

	}	
	function EDIT_SJ(){
			$no_spkb	=	$this->input->get('no_spkb');
				$data['no_spkb'] = $no_spkb;
				$var = '';
				$cabang	=	$this->User->getCabang($this->session->userdata('username'));			
				$nama_cabang =	$this->Cabang->select($cabang[0]->intid_cabang);
				$week = $this->Penjualan->selectWeek();
				
				$data['halaman'] = 'SJ';	
				$data['barang']	=	$this->PO->get_barang();
					
				$data['intid_week']	=	$week[0]->intid_week;
				$data['strnama_cabang']	=	$nama_cabang[0]->strnama_cabang;
				
				$query = $this->PO->OpenDB('select intid_cabang, 
										   (select strnama_cabang from cabang where cabang.intid_cabang = spkb.intid_cabang) strnama_cabang,
											spkb.no_sj,
											(select time waktu from po_ po where po.no_spkb = spkb.no_spkb LIMIT 0,1)tgl_po,
											spkb.*
												from spkb where no_spkb = "'.$data['no_spkb'].'"');
				$result = $query->result();
				$no_po = $result[0]->no_sj;
				$data['intid_cabang']	=	$result[0]->intid_cabang;
				$data['strnama_cabang_spkb'] = $result[0]->strnama_cabang;
				$waktu = date('Y-m-d',strtotime($result[0]->tgl_po));
				$data['waktu'] = $waktu;
				$tgl_kirim = $result[0]->tgl_kirim;
				$via	=	$result[0]->via;
				
				//$data['no_po']	=	$this->PO->get_nopo($result[0]->intid_cabang,$data['intid_week'],'SJ');
				$query	=	$this->PO->OpenDB('select spd.*,barang.strnama,barang.intid_jsatuan 
														  from spkb_detail spd inner join barang on barang.intid_barang = spd.intid_barang 
														  where spd.no_spkb="'.$data['no_spkb'].'" and spd.quantity != 0 order by barang.strnama asc');	
				
				$var .= '<form method="post" action="'.base_url().'POCO/Proses_SJ_STEP4">
						<ul style="list-style-type:none; margin:0 0 0 350px;">
						<li style="margin:auto auto auto -380px;">
							CAB/SC	: <?php echo $strnama_cabang_spkb;?>
							<h3>NO Surat Jalan : '.$no_po.'<input type="hidden" size="2" name="no_sj" value="'.$no_po.'" /><input type="hidden" size="2" name="no_spkb" value="'.$no_spkb.'" /></h3>
							</li>
						</ul>
						<table style="float:right;">
							<tr>
								<td>Bandung</td><td>,</td><td>'.date('d - m - Y').'</td>
							</tr>
							<tr>
								<td>TANGGAL PO</td><td>:</td><td><input type="text" name="tgl_order" id="demo3" size="15" value="'.$waktu.'"/></td>
							</tr>
							<tr>
								<td>TANGGAL SJ</td><td>:</td><td><input type="text" name="tgl_kirim" id="demo4" size="15" value="'.$tgl_kirim.'"/></td>
							</tr>
							<tr>
								<td>PENGIRIMAN VIA</td><td>:</td><td><input type="text" name="via" size="15" value="'.$via.'" /></td>
							</tr>
						</table>
						<table border="1" width="100%" style="background-color:#FFF;">
						<tr>
							<th width="3" rowspan="2">No.</th>
							<th width="30%" rowspan="2">Nama Barang</th>
							<th width="20%" colspan="2">JUMLAH</th>
							<th width="7%" rowspan="2">Quantity Terkirim</th>
							<th width="40%" rowspan="2">Keterangan</th>
						</tr>
						<tr>
							<th>PCS</th>
							<th>SET</th>
						</tr>';
							$i = 0;
							$no = $i+1; 
							$pieces = 0;
							$set = 0;
							foreach($query->result() as $row){
								$var .= "<tr>
									<td align='center'>".$no++."<input type='hidden' name='intid[".$i."]' value='".$row->intid_barang."' size='2' />
									</td>
									<td><input type='text' name='namaBarang[".$i."]' value='".$row->strnama."' size='30' disabled /></td>";
								if($row->intid_jsatuan == 2){
									$pieces = $pieces + $row->quantity;
								$var .= "<td align='center'>".$row->quantity."</td>
									<td align ='center'>0</td>";
								}else{
									$set = $set + $row->quantity;
								$var .= "<td align='center'>0</td><td align='center'>".$row->quantity."</td>";
									}
								$var .= "<td><input type='text' name='quantity[".$i."]' value='".$row->quantity."' size='2' /></td>
									<td><input type='text' name='keterangan[".$i."]' value='".$row->keterangan."' size='30' />
									</td>
								</tr>";
								$i++;
								}
								$var .= "<tr id='content-list'><td colspan='6' align='right'>
								<input type='submit' name='submit' value='CETAK Surat Jalan' style='margin:10px;' />
								</td></tr>";
						$var .= '</table>';
							$var .='<div class="description">
							<p> <small>*</small> Barang terhapus jika quantity di-set menjadi 0 (nol)<small>*</small></p>
							</div><!-- End demo-description -->
								</form>';
						$data['tampilan'] = $var;
					$this->load->view('halaman/po_header');
					$this->load->view('halaman/js',$data);
					$this->load->view('halaman/SJ_hal3',$data);
		}
	function Proses_SJ_STEP4(){
		if($_POST){
				$no_sj	=	$this->input->post('no_sj');
				$no_spkb	=	$this->input->post('no_spkb');
				$namaBarang	=	$this->input->post('namaBarang');
				$intid	=	$this->input->post('intid');
				$quantity	=	$this->input->post('quantity');
				$keterangan	=	$this->input->post('keterangan');
				$tgl_order = $this->input->post('tgl_order');
				$tgl_kirim = $this->input->post('tgl_kirim');
				$via	=	$this->input->post('via');
				
				$TnamaBarang	=	$this->input->post('TnamaBarang');
				$Tintid_barang	=	$this->input->post('Tintid_barang');
				$Tquantity	=	$this->input->post('Tquantity');
				$Tketerangan	=	$this->input->post('Tketerangan');
				
				//mulai update data table
				$this->db->trans_start();
				for($i=0; $i<sizeof($intid);$i++){
					//echo $no_spkb.",".$intid[$i].", ".$namaBarang[$i].",".$quantity[$i].",".$keterangan[$i]."<br />";
					$this->PO->OpenDB('update spkb_detail 
									 	set quantity = "'.$quantity[$i].'", keterangan = "'.$keterangan[$i].'"
										where intid_barang = "'.$intid[$i].'" and no_spkb = "'.$no_spkb.'"');
					$this->db->trans_complete();
				}
				//mulai insert
				for($i=0;$i<sizeof($Tintid_barang);$i++){
					//echo $no_spkb.",".$Tintid_barang[$i].", nama : ".$TnamaBarang[$i].",".$Tquantity[$i].",".$Tketerangan[$i]."::T<br />";
					$this->PO->OpenDB('insert into spkb_detail values("'.$no_spkb.'","'.$Tintid_barang[$i].'","'.$Tquantity[$i].'","'.$Tketerangan[$i].'")');
					$this->db->trans_complete();
				}
				//
				$this->PO->OpenDB('update spkb 
									 	set no_sj = "'.$no_sj.'", tgl_order = "'.$tgl_order.'", tgl_kirim = "'.$tgl_kirim.'", via = "'.$via.'"
										where no_spkb = "'.$no_spkb.'"');
				$this->db->trans_complete();
				$this->PO->OpenDB('delete from spkb_detail
									 	where no_spkb = "'.$no_spkb.'" and quantity = 0');
				$this->db->trans_complete();
				if($this->db->trans_status() == FALSE){
					$this->db->trans_rollback();
					redirect('POCO/Proses_SJ/?no='.$no_spkb.'');	
				}else{
					$this->db->trans_commit();	
					redirect('POCO/GET_SJ/?no_sj='.$no_sj.'&no_spkb='.$no_spkb.'');	
				}
			}
		}
		function cetak_bulanan()
			{
				$nm_cabang = $this->Cabang->select($this->input->post('intid_cabang'));

				$data['cabang'] = $nm_cabang[0]->strnama_cabang;
				$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
				$cabang = $nm_cabang[0]->intid_cabang;
				
				$data['month'] = $this->Laporan->get_Week($this->input->post('month'));
				$data['bulan']	=	$this->nama_bulan($this->input->post('month'));
				$data['week_start'] = $this->Laporan->cek_bulan_start($this->input->post('month'));
				$data['week_end'] = $this->Laporan->cek_bulan_end($this->input->post('month'));
				if ($this->input->post('intid_jpenjualan')==10)
				{ 
					$data['default'] = $this->Laporan->get_CetakPenjualanSKBulanan($this->input->post('month'),$this->input->post('intid_cabang'));
					$this->load->view('admin_views/laporan/penjualan_skbulanan', $data);
				
				}else if ($this->input->post('intid_jpenjualan')==9)
				{ 
					$data['default'] = $this->Laporan->get_CetakPenjualanArisanBulanan($this->input->post('month'),$this->input->post('intid_cabang'));
					$this->load->view('admin_views/laporan/penjualan_arisanbulanan', $data);
				} else {
					$data['default'] = $this->PO->get_CetakPenjualanBulanan($this->input->post('month'),$this->input->post('intid_cabang'), $this->input->post('intid_jpenjualan'));
					
					$this->load->view('halaman/laporan_bulanan', $data);
					//$this->load->view('admin_views/laporan/penjualan_bulanan', $data);
					//$this->load->view('admin_views/laporan/penjualan_mingguan', $data);
				}
			}
			function cetak_mingguan()
			{
				$nm_cabang = $this->Cabang->select($this->input->post('intid_cabang'));

				$data['cabang'] = $nm_cabang[0]->strnama_cabang;
				$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
				$cabang = $nm_cabang[0]->intid_cabang;
				
				if ($this->input->post('intid_jpenjualan')==10)
				{ 
					$data['default'] = $this->Laporan->get_CetakPenjualanSKMingguan($this->input->post('intid_week'),$this->input->post('intid_cabang'));
					$this->load->view('admin_views/laporan/penjualan_skmingguan', $data);
				
				} else if ($this->input->post('intid_jpenjualan')==9)
				{ 
					$data['default'] = $this->Laporan->get_CetakPenjualanArisanMingguan($this->input->post('intid_week'),$this->input->post('intid_cabang'), $this->input->post('intid_jpenjualan'));
					$this->load->view('admin_views/laporan/penjualan_arisanmingguan', $data);
				} else {
					$data['default'] = $this->PO->get_CetakPenjualanMingguan($this->input->post('intid_week'),$this->input->post('intid_cabang'), $this->input->post('intid_jpenjualan'));
					$this->load->view('admin_views/laporan/penjualan_mingguan', $data);
				
				}
				
			}
			function cetak_keumingguan()
			{
				$nm_cabang = $this->Cabang->select($this->input->post('intid_cabang'));
				$data['cabang'] = $nm_cabang[0]->strnama_cabang;
				//$data['reguler'] = $this->PO->get_CetakKeuanganMingguanReguler($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['reguler'] = $this->Laporan->get_CetakKeuanganMingguanReguler($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['hut'] = $this->Laporan->get_CetakKeuanganMingguanHut($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['challenge'] = $this->Laporan->get_CetakKeuanganMingguanChallenge($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['trade'] = $this->Laporan->get_CetakKeuanganMingguanTrade($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['freehut'] = $this->Laporan->get_CetakKeuanganMingguanFreeHut($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['free'] = $this->Laporan->get_CetakKeuanganMingguanFree($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['netto'] = $this->Laporan->get_CetakKeuanganMingguanNetto($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['sk'] = $this->Laporan->get_CetakKeuanganMingguanSK($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['jlain'] = $this->Laporan->get_CetakKeuanganMingguanLain($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['spesial'] = $this->Laporan->get_CetakKeuanganMingguanSpecialPrice($this->input->post('intid_cabang'),$this->input->post('intid_week'));
		//001
				$data['point'] = $this->Laporan->get_CetakKeuanganMingguanPoint($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['metal50'] = $this->Laporan->get_CetakKeuanganMingguanMetal50($this->input->post('intid_cabang'),$this->input->post('intid_week'));

				$data['komisi'] = $this->Laporan->get_CetakKeuanganMingguanKomisi($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['week'] = $this->Laporan->get_DateWeek($this->input->post('intid_week'));
				$data['pembayaran'] = $this->Laporan->selectPembayaranMingguan($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['total_bayar'] = $this->Laporan->selectTotalPembayaranMingguan($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['total_bayartrade'] = $this->Laporan->selectTotalPembayaranMingguanTrade($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['sudah_bayar'] = $this->Laporan->selectTotalBayarMingguan($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['dp'] = $this->Laporan->get_CetakKeuanganMingguanDP($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['komisiarisan'] = $this->Laporan->get_CetakKeuanganMingguanKomisiArisan($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['arisan'] = $this->Laporan->get_CetakKeuanganMingguanArisan($this->input->post('intid_cabang'),$this->input->post('intid_week'));
				$data['lunasdp'] = $this->Laporan->selectTotalDpLunasMingguan($this->input->post('intid_cabang'),$this->input->post('intid_week'));		
				$this->load->view('admin_views/laporan/keuangan_mingguan', $data);
				
			}
	function nama_bulan($bulan) {
	$b="";
        switch ($bulan) {
            case '1':
                $b = "Januari";
                break;
            case '2':
                $b = "Februari";
                break;
            case '3':
                $b = "Maret";
                break;
            case '4':
                $b = "April";
                break;
            case '5':
                $b = "Mei";
                break;
            case '6':
                $b = "Juni";
                break;
            case '7':
                $b = "Juli";
                break;
            case '8':
                $b = "Agustus";
                break;
            case '9':
                $b = "September";
                break;
            case '10':
                $b = "Oktober";
                break;
            case '11':
                $b = "November";
                break;
            case '12':
                $b = "Desember";
                break;

        }

        return $b;

    }
	function sales_cetak()
	{
		$nm_cabang = $this->Cabang->select($this->input->post('intid_cabang'));
	
		$data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$cabang = $nm_cabang[0]->intid_cabang;
		$data['default'] = $this->Laporan->get_CetakSales($this->input->post('intid_week'),$this->input->post('intid_cabang'));
		$this->load->view('admin_views/laporan/sales', $data);
	}
}
?>